package com.shrct.businessprocess.project.impl;

import com.shrct.businessprocess.common.RCGeneralCreditData;
import com.shrct.businessprocess.organization.RCEmployee;
import com.shrct.businessprocess.organization.RCEnterprise;
import com.shrct.businessprocess.project.RCProjectCreditStat;
import com.shrct.businessprocess.project.RCProjectCreditStatService;
import com.shrct.businessprocess.project.RCProjectCreditStatShouldRequestInfo;
import com.shrct.businessprocess.project.RCTotalProjectCreditStat;
import edu.fudan.mylang.common.NumberExtensions;
import edu.fudan.mylang.pf.BaseService;
import edu.fudan.mylang.pf.IGenericQuery;
import edu.fudan.mylang.utils.DateUtil;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import org.eclipse.xtext.xbase.lib.DoubleExtensions;
import org.eclipse.xtext.xbase.lib.Functions.Function1;
import org.eclipse.xtext.xbase.lib.Functions.Function2;
import org.eclipse.xtext.xbase.lib.IterableExtensions;
import org.eclipse.xtext.xbase.lib.ObjectExtensions;
import org.eclipse.xtext.xbase.lib.Procedures.Procedure1;
import org.springframework.stereotype.Service;

@Service("com.shrct.businessprocess.project.RCProjectCreditStatService")
public class RCProjectCreditStatServiceImpl extends BaseService implements RCProjectCreditStatService {
  public RCGeneralCreditData createD() {
    return null;
  }
  
  public Double sumDouble(final Iterable<Double> values) {
    final Function2<Double,Double,Double> _function = new Function2<Double,Double,Double>() {
        public Double apply(final Double d1, final Double d2) {
          double _plus = DoubleExtensions.operator_plus(d1, d2);
          return Double.valueOf(_plus);
        }
      };
    Double _reduce = IterableExtensions.<Double>reduce(values, _function);
    Double _nullsafe = NumberExtensions.nullsafe(_reduce);
    return _nullsafe;
  }
  
  public Integer sumInteger(final Iterable<Integer> values) {
    final Function2<Integer,Integer,Integer> _function = new Function2<Integer,Integer,Integer>() {
        public Integer apply(final Integer d1, final Integer d2) {
          int _plus = ((d1).intValue() + (d2).intValue());
          return Integer.valueOf(_plus);
        }
      };
    Integer _reduce = IterableExtensions.<Integer>reduce(values, _function);
    Integer _nullsafe = NumberExtensions.nullsafe(_reduce);
    return _nullsafe;
  }
  
  public RCTotalProjectCreditStat generateTotalStat(final Collection<RCGeneralCreditData> datas) {
    RCTotalProjectCreditStat _xblockexpression = null;
    {
      Date _day = DateUtil.today();
      int _year = _day.getYear();
      final int thisYear = (_year + 1900);
      final int lastYear = (thisYear - 1);
      RCTotalProjectCreditStat _rCTotalProjectCreditStat = new RCTotalProjectCreditStat();
      final Procedure1<RCTotalProjectCreditStat> _function = new Procedure1<RCTotalProjectCreditStat>() {
          public void apply(final RCTotalProjectCreditStat stat) {
            final Function1<RCGeneralCreditData,Boolean> _function = new Function1<RCGeneralCreditData,Boolean>() {
                public Boolean apply(final RCGeneralCreditData it) {
                  Integer _year = it.getYear();
                  boolean _equals = ((_year).intValue() == thisYear);
                  return Boolean.valueOf(_equals);
                }
              };
            Iterable<RCGeneralCreditData> _filter = IterableExtensions.<RCGeneralCreditData>filter(datas, _function);
            int _size = IterableExtensions.size(_filter);
            stat.setContractCountThisYear(Integer.valueOf(_size));
            final Function1<RCGeneralCreditData,Boolean> _function_1 = new Function1<RCGeneralCreditData,Boolean>() {
                public Boolean apply(final RCGeneralCreditData it) {
                  Integer _year = it.getYear();
                  boolean _equals = ((_year).intValue() == lastYear);
                  return Boolean.valueOf(_equals);
                }
              };
            Iterable<RCGeneralCreditData> _filter_1 = IterableExtensions.<RCGeneralCreditData>filter(datas, _function_1);
            int _size_1 = IterableExtensions.size(_filter_1);
            stat.setContractCountLastYear(Integer.valueOf(_size_1));
            final Function1<RCGeneralCreditData,Boolean> _function_2 = new Function1<RCGeneralCreditData,Boolean>() {
                public Boolean apply(final RCGeneralCreditData it) {
                  Integer _year = it.getYear();
                  boolean _equals = ((_year).intValue() == thisYear);
                  return Boolean.valueOf(_equals);
                }
              };
            Iterable<RCGeneralCreditData> _filter_2 = IterableExtensions.<RCGeneralCreditData>filter(datas, _function_2);
            final Function1<RCGeneralCreditData,Double> _function_3 = new Function1<RCGeneralCreditData,Double>() {
                public Double apply(final RCGeneralCreditData it) {
                  Double _contractAmount = it.getContractAmount();
                  return _contractAmount;
                }
              };
            Iterable<Double> _map = IterableExtensions.<RCGeneralCreditData, Double>map(_filter_2, _function_3);
            Double _sumDouble = RCProjectCreditStatServiceImpl.this.sumDouble(_map);
            stat.setContractAmountThisYear(_sumDouble);
            final Function1<RCGeneralCreditData,Boolean> _function_4 = new Function1<RCGeneralCreditData,Boolean>() {
                public Boolean apply(final RCGeneralCreditData it) {
                  Integer _year = it.getYear();
                  boolean _equals = ((_year).intValue() == lastYear);
                  return Boolean.valueOf(_equals);
                }
              };
            Iterable<RCGeneralCreditData> _filter_3 = IterableExtensions.<RCGeneralCreditData>filter(datas, _function_4);
            final Function1<RCGeneralCreditData,Double> _function_5 = new Function1<RCGeneralCreditData,Double>() {
                public Double apply(final RCGeneralCreditData it) {
                  Double _contractAmount = it.getContractAmount();
                  return _contractAmount;
                }
              };
            Iterable<Double> _map_1 = IterableExtensions.<RCGeneralCreditData, Double>map(_filter_3, _function_5);
            Double _sumDouble_1 = RCProjectCreditStatServiceImpl.this.sumDouble(_map_1);
            stat.setContractAmountLastYear(_sumDouble_1);
            double _xifexpression = (double) 0;
            Double _contractAmountThisYear = stat.getContractAmountThisYear();
            boolean _equals = ((_contractAmountThisYear).doubleValue() == 0d);
            if (_equals) {
              _xifexpression = 0d;
            } else {
              final Function1<RCGeneralCreditData,Boolean> _function_6 = new Function1<RCGeneralCreditData,Boolean>() {
                  public Boolean apply(final RCGeneralCreditData it) {
                    boolean _and = false;
                    Integer _year = it.getYear();
                    boolean _equals = ((_year).intValue() == thisYear);
                    if (!_equals) {
                      _and = false;
                    } else {
                      Boolean _isCurrentEB = it.getIsCurrentEB();
                      _and = (_equals && (_isCurrentEB).booleanValue());
                    }
                    return Boolean.valueOf(_and);
                  }
                };
              Iterable<RCGeneralCreditData> _filter_4 = IterableExtensions.<RCGeneralCreditData>filter(datas, _function_6);
              final Function1<RCGeneralCreditData,Double> _function_7 = new Function1<RCGeneralCreditData,Double>() {
                  public Double apply(final RCGeneralCreditData it) {
                    Double _contractAmount = it.getContractAmount();
                    return _contractAmount;
                  }
                };
              Iterable<Double> _map_2 = IterableExtensions.<RCGeneralCreditData, Double>map(_filter_4, _function_7);
              Double _sumDouble_2 = RCProjectCreditStatServiceImpl.this.sumDouble(_map_2);
              Double _contractAmountThisYear_1 = stat.getContractAmountThisYear();
              double _divide = DoubleExtensions.operator_divide(_sumDouble_2, _contractAmountThisYear_1);
              _xifexpression = _divide;
            }
            stat.setEbPercentThisYear(Double.valueOf(_xifexpression));
            double _xifexpression_1 = (double) 0;
            Double _contractAmountLastYear = stat.getContractAmountLastYear();
            boolean _equals_1 = ((_contractAmountLastYear).doubleValue() == 0d);
            if (_equals_1) {
              _xifexpression_1 = 0d;
            } else {
              final Function1<RCGeneralCreditData,Boolean> _function_8 = new Function1<RCGeneralCreditData,Boolean>() {
                  public Boolean apply(final RCGeneralCreditData it) {
                    boolean _and = false;
                    Integer _year = it.getYear();
                    boolean _equals = ((_year).intValue() == lastYear);
                    if (!_equals) {
                      _and = false;
                    } else {
                      Boolean _isCurrentEB = it.getIsCurrentEB();
                      _and = (_equals && (_isCurrentEB).booleanValue());
                    }
                    return Boolean.valueOf(_and);
                  }
                };
              Iterable<RCGeneralCreditData> _filter_5 = IterableExtensions.<RCGeneralCreditData>filter(datas, _function_8);
              final Function1<RCGeneralCreditData,Double> _function_9 = new Function1<RCGeneralCreditData,Double>() {
                  public Double apply(final RCGeneralCreditData it) {
                    Double _contractAmount = it.getContractAmount();
                    return _contractAmount;
                  }
                };
              Iterable<Double> _map_3 = IterableExtensions.<RCGeneralCreditData, Double>map(_filter_5, _function_9);
              Double _sumDouble_3 = RCProjectCreditStatServiceImpl.this.sumDouble(_map_3);
              Double _contractAmountLastYear_1 = stat.getContractAmountLastYear();
              double _divide_1 = DoubleExtensions.operator_divide(_sumDouble_3, _contractAmountLastYear_1);
              _xifexpression_1 = _divide_1;
            }
            stat.setEbPercentLastYear(Double.valueOf(_xifexpression_1));
            final Function1<RCGeneralCreditData,Boolean> _function_10 = new Function1<RCGeneralCreditData,Boolean>() {
                public Boolean apply(final RCGeneralCreditData it) {
                  Boolean _isCurrentEB = it.getIsCurrentEB();
                  return _isCurrentEB;
                }
              };
            Iterable<RCGeneralCreditData> _filter_6 = IterableExtensions.<RCGeneralCreditData>filter(datas, _function_10);
            int _size_2 = IterableExtensions.size(_filter_6);
            stat.setEbCurrentCount(Integer.valueOf(_size_2));
            final Function1<RCGeneralCreditData,Boolean> _function_11 = new Function1<RCGeneralCreditData,Boolean>() {
                public Boolean apply(final RCGeneralCreditData it) {
                  Boolean _isCurrentEB = it.getIsCurrentEB();
                  return _isCurrentEB;
                }
              };
            Iterable<RCGeneralCreditData> _filter_7 = IterableExtensions.<RCGeneralCreditData>filter(datas, _function_11);
            final Function1<RCGeneralCreditData,Double> _function_12 = new Function1<RCGeneralCreditData,Double>() {
                public Double apply(final RCGeneralCreditData it) {
                  Double _contractAmount = it.getContractAmount();
                  return _contractAmount;
                }
              };
            Iterable<Double> _map_4 = IterableExtensions.<RCGeneralCreditData, Double>map(_filter_7, _function_12);
            Double _sumDouble_4 = RCProjectCreditStatServiceImpl.this.sumDouble(_map_4);
            stat.setEbCurrentAmount(_sumDouble_4);
            final Function1<RCGeneralCreditData,Integer> _function_13 = new Function1<RCGeneralCreditData,Integer>() {
                public Integer apply(final RCGeneralCreditData it) {
                  Integer _ebDays = it.getEbDays();
                  return _ebDays;
                }
              };
            Iterable<Integer> _map_5 = IterableExtensions.<RCGeneralCreditData, Integer>map(datas, _function_13);
            final Function1<Integer,Boolean> _function_14 = new Function1<Integer,Boolean>() {
                public Boolean apply(final Integer it) {
                  boolean _greaterThan = ((it).intValue() > 0);
                  return Boolean.valueOf(_greaterThan);
                }
              };
            Iterable<Integer> _filter_8 = IterableExtensions.<Integer>filter(_map_5, _function_14);
            final Function1<Integer,Integer> _function_15 = new Function1<Integer,Integer>() {
                public Integer apply(final Integer it) {
                  return it;
                }
              };
            List<Integer> _sortBy = IterableExtensions.<Integer, Integer>sortBy(_filter_8, _function_15);
            Integer _last = IterableExtensions.<Integer>last(_sortBy);
            Integer _nullsafe = NumberExtensions.nullsafe(_last);
            stat.setEbMaxLength(_nullsafe);
            final Function1<RCGeneralCreditData,Integer> _function_16 = new Function1<RCGeneralCreditData,Integer>() {
                public Integer apply(final RCGeneralCreditData it) {
                  Integer _ebDays = it.getEbDays();
                  return _ebDays;
                }
              };
            Iterable<Integer> _map_6 = IterableExtensions.<RCGeneralCreditData, Integer>map(datas, _function_16);
            final Function1<Integer,Boolean> _function_17 = new Function1<Integer,Boolean>() {
                public Boolean apply(final Integer it) {
                  boolean _greaterThan = (it.compareTo(Integer.valueOf(0)) > 0);
                  return Boolean.valueOf(_greaterThan);
                }
              };
            Iterable<Integer> _filter_9 = IterableExtensions.<Integer>filter(_map_6, _function_17);
            List<Integer> _sort = IterableExtensions.<Integer>sort(_filter_9);
            final Procedure1<List<Integer>> _function_18 = new Procedure1<List<Integer>>() {
                public void apply(final List<Integer> it) {
                  final int s = it.size();
                  int _xifexpression = (int) 0;
                  boolean _equals = (s == 0);
                  if (_equals) {
                    _xifexpression = 0;
                  } else {
                    List<Integer> _list = IterableExtensions.<Integer>toList(it);
                    int _divide = (s / 2);
                    Integer _get = _list.get(_divide);
                    _xifexpression = _get;
                  }
                  stat.setEbMidLength(Integer.valueOf(_xifexpression));
                }
              };
            ObjectExtensions.<List<Integer>>operator_doubleArrow(_sort, _function_18);
            final Function1<RCGeneralCreditData,Double> _function_19 = new Function1<RCGeneralCreditData,Double>() {
                public Double apply(final RCGeneralCreditData it) {
                  Double _noInvoiceAmount = it.getNoInvoiceAmount();
                  return _noInvoiceAmount;
                }
              };
            Iterable<Double> _map_7 = IterableExtensions.<RCGeneralCreditData, Double>map(datas, _function_19);
            Double _sumDouble_5 = RCProjectCreditStatServiceImpl.this.sumDouble(_map_7);
            stat.setNoInvoiceAmount(_sumDouble_5);
            final Function1<RCGeneralCreditData,Double> _function_20 = new Function1<RCGeneralCreditData,Double>() {
                public Double apply(final RCGeneralCreditData it) {
                  RCProjectCreditStatShouldRequestInfo _shouldRequestInfo = it.getShouldRequestInfo();
                  Double _shouldRequestAmount = _shouldRequestInfo.getShouldRequestAmount();
                  return _shouldRequestAmount;
                }
              };
            Iterable<Double> _map_8 = IterableExtensions.<RCGeneralCreditData, Double>map(datas, _function_20);
            Double _sumDouble_6 = RCProjectCreditStatServiceImpl.this.sumDouble(_map_8);
            stat.setShouldRequestAmount(_sumDouble_6);
            final Function1<RCGeneralCreditData,Double> _function_21 = new Function1<RCGeneralCreditData,Double>() {
                public Double apply(final RCGeneralCreditData it) {
                  RCProjectCreditStatShouldRequestInfo _shouldRequestInfo = it.getShouldRequestInfo();
                  Double _amount90 = _shouldRequestInfo.getAmount90();
                  return _amount90;
                }
              };
            Iterable<Double> _map_9 = IterableExtensions.<RCGeneralCreditData, Double>map(datas, _function_21);
            Double _sumDouble_7 = RCProjectCreditStatServiceImpl.this.sumDouble(_map_9);
            stat.setAmount90(_sumDouble_7);
            final Function1<RCGeneralCreditData,Double> _function_22 = new Function1<RCGeneralCreditData,Double>() {
                public Double apply(final RCGeneralCreditData it) {
                  RCProjectCreditStatShouldRequestInfo _shouldRequestInfo = it.getShouldRequestInfo();
                  Double _amount91_180 = _shouldRequestInfo.getAmount91_180();
                  return _amount91_180;
                }
              };
            Iterable<Double> _map_10 = IterableExtensions.<RCGeneralCreditData, Double>map(datas, _function_22);
            Double _sumDouble_8 = RCProjectCreditStatServiceImpl.this.sumDouble(_map_10);
            stat.setAmount91_180(_sumDouble_8);
            final Function1<RCGeneralCreditData,Double> _function_23 = new Function1<RCGeneralCreditData,Double>() {
                public Double apply(final RCGeneralCreditData it) {
                  RCProjectCreditStatShouldRequestInfo _shouldRequestInfo = it.getShouldRequestInfo();
                  Double _amount181_1860 = _shouldRequestInfo.getAmount181_1860();
                  return _amount181_1860;
                }
              };
            Iterable<Double> _map_11 = IterableExtensions.<RCGeneralCreditData, Double>map(datas, _function_23);
            Double _sumDouble_9 = RCProjectCreditStatServiceImpl.this.sumDouble(_map_11);
            stat.setAmount181_1860(_sumDouble_9);
            final Function1<RCGeneralCreditData,Double> _function_24 = new Function1<RCGeneralCreditData,Double>() {
                public Double apply(final RCGeneralCreditData it) {
                  RCProjectCreditStatShouldRequestInfo _shouldRequestInfo = it.getShouldRequestInfo();
                  Double _amount1861 = _shouldRequestInfo.getAmount1861();
                  return _amount1861;
                }
              };
            Iterable<Double> _map_12 = IterableExtensions.<RCGeneralCreditData, Double>map(datas, _function_24);
            Double _sumDouble_10 = RCProjectCreditStatServiceImpl.this.sumDouble(_map_12);
            stat.setAmount1861(_sumDouble_10);
            final Function1<RCGeneralCreditData,Boolean> _function_25 = new Function1<RCGeneralCreditData,Boolean>() {
                public Boolean apply(final RCGeneralCreditData it) {
                  Boolean _isBadProject = it.getIsBadProject();
                  return _isBadProject;
                }
              };
            Iterable<RCGeneralCreditData> _filter_10 = IterableExtensions.<RCGeneralCreditData>filter(datas, _function_25);
            int _size_3 = IterableExtensions.size(_filter_10);
            stat.setIsBadProjectCount(Integer.valueOf(_size_3));
            final Function1<RCGeneralCreditData,Boolean> _function_26 = new Function1<RCGeneralCreditData,Boolean>() {
                public Boolean apply(final RCGeneralCreditData it) {
                  Boolean _isBadProject = it.getIsBadProject();
                  return _isBadProject;
                }
              };
            Iterable<RCGeneralCreditData> _filter_11 = IterableExtensions.<RCGeneralCreditData>filter(datas, _function_26);
            final Function1<RCGeneralCreditData,Double> _function_27 = new Function1<RCGeneralCreditData,Double>() {
                public Double apply(final RCGeneralCreditData it) {
                  RCProjectCreditStatShouldRequestInfo _shouldRequestInfo = it.getShouldRequestInfo();
                  Double _shouldRequestAmount = _shouldRequestInfo.getShouldRequestAmount();
                  return _shouldRequestAmount;
                }
              };
            Iterable<Double> _map_13 = IterableExtensions.<RCGeneralCreditData, Double>map(_filter_11, _function_27);
            Double _sumDouble_11 = RCProjectCreditStatServiceImpl.this.sumDouble(_map_13);
            stat.setIsBadProjectAmount(_sumDouble_11);
          }
        };
      final RCTotalProjectCreditStat result = ObjectExtensions.<RCTotalProjectCreditStat>operator_doubleArrow(_rCTotalProjectCreditStat, _function);
      _xblockexpression = (result);
    }
    return _xblockexpression;
  }
  
  public Collection<RCProjectCreditStat> getProjectCreditStat(final RCEnterprise customer) {
    IGenericQuery<RCProjectCreditStat> query=getObjectFactory().createGenericQuery(RCProjectCreditStat.class,"select e from com.shrct.businessprocess.project.impl.RCProjectCreditStatImpl as e   where ((e.project.customer) = (:customer)) and  e.active = 1      ").setParameter("customer",customer);
    Collection<RCProjectCreditStat> _list = query.list();
    return _list;
  }
  
  public Collection<RCProjectCreditStat> getProjectCreditStat(final RCEmployee saler) {
    IGenericQuery<RCProjectCreditStat> query=getObjectFactory().createGenericQuery(RCProjectCreditStat.class,"select e from com.shrct.businessprocess.project.impl.RCProjectCreditStatImpl as e   where ((e.majorSaler) = (:saler)) and  e.active = 1      ").setParameter("saler",saler);
    Collection<RCProjectCreditStat> _list = query.list();
    return _list;
  }
}
